package com.filldream.fastboot.common.constant.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;

/**
 * 存在差异的股票和指数
 */
@Getter
@AllArgsConstructor
public enum StockSearchCodeEnum {

    IXIC("IXIC","NDX", "IXIC","100","88")
    ,SPX("SPX","SPX", "INX","100","88")
    ,DJI("DJI","DJIA", "DJI","100","88")
    ,HSTECH("HS2083","HSTECH", "HSTECH","124","176")
    ,HSCEI("HSCEI","HSCEI", "HSCEI","100","hk")
    ,HSI("HSI","HSI", "HSI","100","hk")
    ,HXC("HXC","HXC", "HXC","251","88")
    ,SZZS("1A0001","000001", "000001","1","hs")
    ,ZZ1000("1B0852","000852", "000852","1","hs")
    ,BZ50("899050","899050", "899050","0","144")
    ,KCZJ("1B0680","000680", "000680","1","hs")
    ,KC50("1B0688","000688", "000688","1","hs")
    ,ZZA500("1B0510","000510", "000510","1","hs")
    ,HS300("1B0300","000300", "000300","1","hs")
    ,SZ50("1B0016","000016", "000016","1","hs")
    ,ZZ500("1B0905","000905", "000905","1","hs")
    ;

    //同花顺的代码
    private String thsCode;

    //东财代码
    private String cfCode;

    //腾讯代码
    private String txCode;

    //东财图片前缀代码
    private String cfImgType;

    //同花顺的K线代码
    private String thsKLineType;

    /**
     * 校验腾讯代码的前缀
     * @param searchCode    腾讯代码
     * @return  true-正常代码 false-异常代码
     */
    public static Boolean checkTxCode(String searchCode){
        if(StringUtils.isBlank(searchCode)){
            return false;
        }
        if( searchCode.length() < 3 ){
            return false;
        }
        if(!Arrays.asList(  "us", "hk", "sh", "sz", "bj","uk"  ).contains(searchCode.substring(0,2)) ){
            return false;
        }
        return true;
    }

    /**
     * 根据腾讯搜索代码获取对应的代码
     * @param searchCode    搜索代码，例如：sh000001
     * @param type          按XX匹配类型
     * @return
     */
    public static StockSearchCodeEnum findBySearchCode(String searchCode,Integer type) {
        if(!checkTxCode(searchCode)){
            return null;
        }
        final String simpleCode = searchCode.substring(2).replaceAll("\\.","");
        for (StockSearchCodeEnum item : StockSearchCodeEnum.values()) {
            if( type.equals(1) && item.getThsCode().equals(simpleCode) ){
                return item;
            }
            if( type.equals(2) && item.getCfCode().equals(simpleCode) ){
                return item;
            }
            if( type.equals(3) && item.getTxCode().equals(simpleCode) ){
                return item;
            }
        }
        return null;
    }

}
